/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.braincasedeisgns.roller.web.api;

import com.braincasedesigns.roller.factory.DieFactory;
import com.braincasedesigns.roller.model.DicePool;
import com.braincasedesigns.roller.model.DieRollResult;
import java.io.*;

import java.util.List;

import javax.imageio.spi.ServiceRegistry;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author jcampbel
 */
public class RollServlet extends HttpServlet {
    
    //private static ServiceRegistry //<DieFactory> dieFactoryLoader = ServiceProvider.load(DieFactory.class);

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/plain;charset=UTF-8");

        //Use ServiceLoader in 1.6
        DieFactory dieFactory = ServiceRegistry.lookupProviders(DieFactory.class).next();
                //dieFactoryLoader.iterator().next();

        PrintWriter out = response.getWriter();
        try {
            StringBuilder junk = new StringBuilder();
            junk.append(request.getContextPath());
            junk.append(request.getServletPath());


            String rollText = request.getRequestURI().substring(junk.length() + 1);

            List<DicePool> pools = null;
            StringBuilder currentResultsText = new StringBuilder();
            try {
                pools = dieFactory.getRoll(rollText);
            } catch (Throwable ex) {
                currentResultsText.append(rollText).append(":").append(ex.getMessage()).append("\n");
            }
            if (pools != null) {
                for (DicePool p : pools) {
                    List<DieRollResult> results = p.roll();
                    for (DieRollResult r : results) {
                        currentResultsText.append(r.getDieRoll().getNotation()).append(":").append(r.getStringTotal()).append("\n");
                    }
                }
            }

            out.println(currentResultsText.toString());


        /* TODO output your page here
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet RollServlet</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet RollServlet at " + request.getContextPath () + "</h1>");
        out.println("</body>");
        out.println("</html>");
         */
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
